//
//  CXBaseViewController.m
//  base_test
//
//  Created by  androidlongs on 17/8/9.
//  Copyright © 2017年  androidlongs. All rights reserved.
//

#import "CXBaseViewController.h"
#import "SINAUiviewClickLiserner.h"
#import "SINAStubarUtils.h"
#import "SINALoginViewController.h"
#import "SINANavLoginViewController.h"
#import "SINAStuApproveUserNameViewController.h"
#import "SINAControllerUtils.h"
#import "SINAStudentTabBarViewController.h"

@interface CXBaseViewController ()

@property(nonatomic) BOOL isLoading;
@end

@implementation CXBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configNavigationItem];
    [self initBackItem];
    
    //    self.loadingView =[YPXLoaddingView initWithParentView:self.view];
//    WeakSelf;
//    [[SINAUiviewClickLiserner alloc]viewClickFunction:self.view andWithBlick:^(UIView *view) {
//        [weakSelf.view endEditing:YES];
//    }];
    
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    UIViewController *tabbarController = window.rootViewController;
    
    if ([tabbarController isKindOfClass:[SINAStudentTabBarViewController class]]) {
        if (UI_IS_IPHONE6PLUS) {
            [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bar_plus_bg"]  forBarMetrics:UIBarMetricsDefault];
        } else {
            [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bar_bg"]  forBarMetrics:UIBarMetricsDefault];
        }
    }
}
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
-(void)configNavigationItem{
    //设置状态栏颜色为白色
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    self.navigationController.navigationBar.barStyle = UIStatusBarStyleLightContent;
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0/255 green:198.0/255 blue:174.0/255 alpha:0.8f];
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],
                                                                      NSForegroundColorAttributeName:[UIColor whiteColor]}];
}
-(void)initBackItem{
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:0 target:self action:@selector(dismiss)];
    //backItem.title = @"返回";
    backItem.image = [UIImage imageNamed:@"back1"];
    self.navigationItem.leftBarButtonItem = backItem;
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
}
-(void)hiniBackItem{
    self.navigationItem.leftBarButtonItem =nil;
}
-(void)hiniNavItem{
    self.navigationController.navigationBarHidden = YES;
}
-(void)initNavBarColorString:(NSString*) color{
    [self initNavBarColor:[UIColor colorWithHexString:color]];
}
-(void)initNavBarColor:(UIColor*) color{
    [SINAStubarUtils setStatusBarBackgroundColor:color];
}
-(void)showNavItem{
    self.navigationController.navigationBarHidden = NO;
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    if (self.navigationController.viewControllers.count > 1) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }else{
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

-(void)dismiss{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//校验身份验证
-(BOOL) checkApproveIdent{
    WeakSelf;
    SINAUserModel *userModel = [[SINACommonUserHelper share]getUserModel];
    if (userModel==nil) {
        //重新登录
        SINALoginViewController *rootViewControll = [[SINALoginViewController alloc]init ];
        SINANavLoginViewController *homeController = [[SINANavLoginViewController alloc] initWithRootViewController:rootViewControll];
        [[SINAControllerUtils alloc]restoreRootViewController:homeController andWithWindow:self.view.window];
        return NO;
    } else {
        //校验
        NSString *universityid = userModel.universityid;
        if (universityid==nil||[universityid isEqualToString:@"(null)"]||universityid.length==0||[universityid integerValue]==0||[universityid integerValue]==-1) {
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"需要认证身份" sureBtn:@"确定" cancleBtn:@"取消"];
            alertView.resultIndex = ^(NSInteger index) {
                SINAStuApproveUserNameViewController *stuApproveUserNameController = [[SINAStuApproveUserNameViewController alloc]initWithNibName:@"SINAStuApproveUserNameViewController" bundle:nil];
                stuApproveUserNameController.openType=1;
                stuApproveUserNameController.hidesBottomBarWhenPushed=YES;
                [weakSelf.navigationController pushViewController:stuApproveUserNameController animated:YES];
            };
            [alertView showXLAlertView];
            return NO;
        }
        return YES;
    }
}

-(void)startLoading
{
    if (!self.isLoading) {
        self.isLoading=YES;
        self.ptLoadingView =[PTLoadingView loadingViewAndStartAnimationWithFrame:self.view.frame title:nil InView:self.view];
    }
}
-(void)startLoadingWithOrginY:(NSInteger)yFlag{
    if (!self.isLoading) {
        self.isLoading=YES;
        CGRect preFrame=self.view.frame;
        preFrame.origin.y+=yFlag;
        self.ptLoadingView =[PTLoadingView loadingViewAndStartAnimationWithFrame:preFrame title:nil InView:self.view];
    }
}
-(void)stopLoading
{
    if (self.isLoading) {
        [self.ptLoadingView.animationIndicator stopAnimation];
        [self.ptLoadingView removeFromSuperview];
        self.isLoading=NO;
    }
}

-(void)createView
{
    
}
-(void)requestData
{
    
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
